<?php
/**
 * DESCRIPTION:
 *  basic html output class
 * FIELDS:
 *  public $fields is an array of form's elements
 *  protected $submit is for Submit button caption
 *  protected $td_class is a class for td
 *  protected $td_style is a css sytle for td
 *  protected $field_class is a class for field
 *  protected $field_style is a css style for field
 * METHODS:
 *  public void print_header($page_title='Безымянная страничка', $page_enc='Win-1251', $stylesheets='') prints the HTML headers
 *  public void print_footer() prints the HTML footer
 *  public void print_sidebar() prints sidebar
 * ======
 * @author at8eqeq3
 */
class htmlout{

    private static $visualtheme;

    public static function print_header($page_title='Безымянная страничка', $page_enc='UTF-8', $stylesheets='', $javascripts=''){
        echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
        echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" dir=\"ltr\" lang=\"ru-RU\">\n";
        echo " <head>\n";
        echo "  <title>$page_title</title>\n";
        echo "  <meta http-equiv=\"Content-type\" content=\"text/html; charset=$page_enc\" />\n";
        echo "  <link rel=\"stylesheet\" type=\"text/css\" href=\"/visuals/".htmlout::$visualtheme."/style.css\" />\n";
        if(is_array($stylesheets)){
            foreach($stylesheets as $val){
                echo "  <link rel=\"stylesheet\" type=\"text/css\" href=\"/$val\" />\n";
            }
        }elseif($stylesheets!=''){
            echo "  <link rel=\"stylesheet\" type=\"text/css\" href=\"/$stylesheets\" />\n";
        }
        if(is_array($javascripts)){
            foreach($javascripts as $val){
                echo "  <script type=\"text/javascript\" src=\"/$val\"></script>\n";
            }
        }elseif($stylesheets!=''){
            echo "  <script type=\"text/javascript\" src=\"/$javascripts\"></script>\n";
        }
        echo " </head>\n";
        echo " <body>\n";
        echo "  <div id=\"bounds\">";
        echo "<!-- print_header output finished -->\n";
    }

    public static function print_footer(){
        echo "\n<!-- print_footer output started -->\n";
        echo "  </div>";
        echo " </body>\n";
        echo "</html>";
    }

    public static function print_sidebar($htmlid = ''){
        $sidebar = new sidebar(WIDGETS);
        if($htmlid == ''){
            $htmlid = 'sidebar';
        }
        echo "\n<!-- print_sidebar output started -->\n";
        echo "<div class=\"sidebar\" id=\"$htmlid\">\n";
        $sidebar->print_widgets();
        echo "</div>\n";
        echo "<!-- print_sidebar output finished -->\n";
    }

    public static function print_pageheader(){
        if(!@include "visuals/".htmlout::$visualtheme."/header.php"){
            $err = new error(__FILE__, __LINE__, "Не удалось подключить шаблон заголовка", true);
            $err->raise();
        }
    }

    public static function print_pagecontent($module){
        echo "<!-- content output started -->\n";
        echo "<div id=\"content\">\n";
        global $cur_user;
        $inclusion = $module->fields['id'];
        if($module->fields['disabled'] == 1){
          array_push($_SESSION['errmsg']['global'], "Ыщщщ! Эту страничку еще не достроили...</a>");
          $inclusion = 'disabled';
        }else{
          if($module->fields['accesslevel'] > $cur_user->fields['accesslevel']){
              if($module->fields['redirect']){
                  $inclusion = $module->fields['redirect'];
              }else{
                  $inclusion = 'login';
                  global $back;
                  $back = $module->fields['id'];
              }
              array_push($_SESSION['errmsg']['global'], "Доступ к странице запрещен. Войдите или <a href=\"/register\">зарегистрируйтесь</a>");
              //echo "<div class=\"error\">Доступ к странице запрещен. Войдите или <a href=\"/register\">зарегистрируйтесь</a></div>\n";
          }
        }
        if(!empty($_SESSION['errmsg']['global'])){
            echo "<div class=\"page_errmsg\">\nОй! Что-то у нас не так:\n <ul>\n";
            foreach($_SESSION['errmsg']['global'] as $f => $e){
                echo "  <li>$e</li>\n";
            }
            echo " </ul>\n</div>\n";
            unset($_SESSION['errmsg']['global']);
        }
        if(!@include("modules/$inclusion.php")){
            $err = new error(__FILE__, __LINE__, "Не удалось подключить модуль <b>".$module->fields['header']."</b>", false);
            $err->raise();
        }
        
        
        //$s = file_get_contents("visuals/".htmlout::$visualtheme."/fish.txt");
        //echo $s;
        echo "</div>\n";
        echo "<!-- content output finished -->\n";
    }

    public static function print_pagefooter(){
        if(!@include "visuals/".htmlout::$visualtheme."/footer.php"){
            $err = new error(__FILE__, __LINE__, "Не удалось подключить шаблон заголовка", true);
            $err->raise();
        }
    }

    public static function set_theme($theme){
        htmlout::$visualtheme = $theme;
    }

    public static function smartlogo(){
        echo "<div id=\"logo\">";
        if($_GET['module'] != 'main'){
            echo " <a href=\"/\">";
        }
        echo "<img src=\"/visuals/".htmlout::$visualtheme."/images/logo.png\" />";
        if($_GET['module'] != 'main'){
            echo " </a>";
        }
        echo "</div>\n";
    }
}
?>